// const user = require('../controllers/user')
const socketController = require('../controllers/socket')
const rp = require('request-promise')
// const group = require('../controllers/group')
const { formatBody } = require('../tools')

const initSocketListener = io => {
  io.sockets.on('connection', socket => {
    console.log(`************new connection************`)
    // 登录
    // socket.on('login', (info, cb) => {
    //   user.login(info, socket, cb)
    // })
    // // 自动登录
    // socket.on('autoLogin', (info, cb) => {
    //   user.autoLogin(info, socket, cb)
    // })
    // // 注册
    // socket.on('signUp', (info, cb) => {
    //   user.signUp(info, socket, cb)
    // })
    // // 下线
    // socket.on('disconnect', (info) => {
    //   socketController.disconnect(info, socket, io)
    // })
    // 加入群组
    socket.on('joinGroup', (info, cb) => {
      socketController.joinGroup(info, socket, cb)
    })
    // 发送新消息
    socket.on('groupMsg', async (info, cb) => {
      await socketController.groupMsg(info, socket, cb, io)
    })
    socket.on('getMsg', async (info, cb) => {
      await socketController.getMsg(info, socket, cb, io)
    })
    socket.on('foul', async (info, cb) => {
      await socketController.foul(info, socket, cb, io)
    })
    socket.on('cancelFoul', async (info, cb) => {
      await socketController.cancelFoul(info, socket, cb, io)
    })
    socket.on('moveBar', (info, cb) => {
      io.sockets.emit('moveBar', info)
      cb(info)
      // socketController.joinGroup(info, socket, cb)
    })
    socket.on('shootBall', (info, cb) => {
      io.sockets.emit('shootBall', info)
      cb(info)
      // socketController.joinGroup(info, socket, cb)
    })
    socket.on('gameMsg', (info, cb) => {
      console.log('gameMsg===>', info)
      io.sockets.emit('gameMsg', info)
      if (cb) cb(info)
      // socketController.joinGroup(info, socket, cb)
    })
    socket.on('gameOver', async (info, cb) => {
      console.log('gameOver===>', info)
      const { req_url, req_data, lottery_url, lottery_data } = info
      await rp({
        method: 'POST',
        uri: req_url,
        body: req_data,
        json: true
      }).then(async res => {
        if (res.status === 1) {
          if (lottery_url) {
            await rp({
              method: 'POST',
              uri: lottery_url,
              body: lottery_data,
              json: true
            }).then(async res1 => {
              console.log('res1=>', res1)
              io.sockets.emit('gameOver', {
                ...info,
                res_data: res,
                lottery_res: res1,
                status: 1
              })
              if (cb) {
                cb({
                  ...info,
                  res_data: res,
                  lottery_res: res1,
                  status: 1
                })
              }
            }).catch(err => {
              io.sockets.emit('gameOver', {
                ...info,
                res_data: res,
                lottery_res: {
                  err,
                  message: '抽奖接口调用失败',
                  status: 1
                },
                status: 1
              })
              if (cb) {
                cb({
                  ...info,
                  res_data: res,
                  status: 1
                })
              }
            })
          } else {
            io.sockets.emit('gameOver', {
              ...info,
              res_data: res,
              status: 1
            })
            if (cb) {
              cb({
                ...info,
                res_data: res,
                status: 1
              })
            }
          }
        } else {
          io.sockets.emit('gameOver', {
            ...info,
            res_data: res,
            status: 0
          })
          if (cb) {
            cb({
              ...info,
              res_data: res,
              status: 0
            })
          }
        }
      }).catch(err => {
        io.sockets.emit('gameOver', {
          ...info,
          status: 0
        })
        if (cb) {
          cb({
            ...info,
            status: 0
          })
        }
      })
      // socketController.joinGroup(info, socket, cb)
    })
  })
}
module.exports = initSocketListener
